package com.shenghui.enterprise.config.webmvc;

import com.shenghui.enterprise.interceptor.AuthorizeInterceptor;
import com.shenghui.enterprise.interceptor.LoginInterceptor;
import com.shenghui.enterprise.utils.DateConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.http.CacheControl;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

/**
 * @Description: WebConfig信息
 * @Author houjh
 * @Email: happyxiaohou@gmail.com
 * @Date: 2020-05-29 9:17
 * @Copyright (c) 2020-2022 云南昇晖科技有限公司
 */
@Configuration
public class WebConfigurer implements WebMvcConfigurer {

    @Autowired
    private RequestMappingHandlerAdapter handlerAdapter;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 拦截所有请求，通过判断是否有 @LoginRequired 注解 决定是否需要登录
        registry.addInterceptor(loginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(
                    "/",
                    "/index",
                    "/admin/login",
                    "/common/validateImage",
                    "/front/css/bootstrap.css.map",
                    "/front/entBasicInfo/register",
                    "/front/login/login",
                    "/admin/baseDataDictionary/getDataDictionaryDetailListByCode",
                    "/html/admin/base/login/login.html",
                    "/html/front/login/login.html",
                    "/html/front/register/register.html"
                ).excludePathPatterns(
                    "/**/*.css",
                    "/**/*.js",
                    "/**/*.png",
                    "/**/*.jpg",
                    "/**/*.jpeg",
                    "/**/*.gif",
                    "/**/*.ico",
                    "/**/*.json",
                    "/**/*.svg",
                    "/**/font/*"
                );

        registry.addInterceptor(authorityInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(
                    "/",
                    "/index",
                    "/admin/login",
                    "/common/validateImage",
                    "/front/css/bootstrap.css.map",
                    "/front/entBasicInfo/register",
                    "/front/login/login",
                    "/admin/baseDataDictionary/getDataDictionaryDetailListByCode"
                ).excludePathPatterns(
                    "/favicon.ico",
                    "/**/*.css",
                    "/**/*.js",
                    "/**/*.html",
                    "/**/*.png",
                    "/**/*.jpg",
                    "/**/*.jpeg",
                    "/**/*.gif",
                    "/**/*.doc",
                    "/**/*.docx",
                    "/**/*.pdf",
                    "/**/*.ico",
                    "/**/*.json",
                    "/**/*.xml",
                    "/**/*.svg",
                    "/**/font/*",
                    "/**/html/*",
                    "/html/*"
                );
    }
    
    /** 
     * 精确设定缓存资源。使用配置文件可以粗略设置缓存时间。
     * 指定png、jpg、jpeg、gif结尾的文件，缓存时间为24小时。
     * 生产环境得去了
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("/**/*.png","/**/*.jpg","/**/*.jpeg","/**/*.gif")
                .addResourceLocations("classpath:/static/")
                .setCacheControl(CacheControl.maxAge(0, TimeUnit.HOURS).cachePublic());
    }

    /**
     * 增加字符串转日期的功能
     */
    @PostConstruct
    public void initEditableAvlidation() {
        ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) handlerAdapter.getWebBindingInitializer();
        if (initializer.getConversionService() != null) {
            GenericConversionService genericConversionService = (GenericConversionService)initializer.getConversionService();
            genericConversionService.addConverter(new DateConvert());
        }
    }

    @Bean
    public LoginInterceptor loginInterceptor() {
        return new LoginInterceptor();
    }

    @Bean
    public AuthorizeInterceptor authorityInterceptor() {
        return new AuthorizeInterceptor();
    }


    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        WebMvcConfigurer.super.configurePathMatch(configurer);
        configurer.setUseSuffixPatternMatch(false);
    }

}
